package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: XJH
 * @Description: TODO
 * @Date: 2025/6/5 12:12
 * @Version: 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private  SetmealDishMapper setmealDishMapper;
    /**
     * 新增套餐以及套餐包含的菜品
     * @param setmealDTO
     * @return
     */
    @Transactional
    @Override
    public void saveWithsetmealDishes(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //向套餐表中插入数据
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        //向每个套餐中添加菜品   套餐有setmealId 还有 菜品有dishId
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes!=null&&setmealDishes.size()>0)
        {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }

    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
       PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
       Page<Setmeal> page= setmealMapper.pageQuery(setmealPageQueryDTO);
       return new PageResult(page.getTotal(), page.getResult());
    }


    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @Override
    public void DeleteByIds(List<Long> ids) {
        setmealMapper.DeleteByIds(ids);
    }

    /**
     * 修改套餐
     * @param setmealVO
     * @return
     */
    @Override
    public void update(SetmealVO setmealVO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmealMapper.update(setmeal);
        Long setmealId = setmeal.getId();

        //先删除再插入新的数据
        setmealDishMapper.DeleteByDishId(setmeal.getId());
        //插入新的数据
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        if(setmealDishes!=null&& !setmealDishes.isEmpty()){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setDishId(setmealId);
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }
    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
       Setmeal setmeal =setmealMapper.getById(id);
    List<SetmealDish> setmealDish =  setmealDishMapper.getBySetmealId(setmeal.getId());
    //封装后端传递给前端的数据
        SetmealVO setmealVO = new SetmealVO();
       BeanUtils.copyProperties(setmeal,setmealVO);
       setmealVO.setSetmealDishes(setmealDish);
        return setmealVO;
    }

    /**
     * 套餐起售、停售
     * @param status
     */
    @Override
    public void startOrStop(Integer status,  long id) {
        setmealMapper.startOrStop(status,  id);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }


    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
